package com.plant.action;

import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.plant.entity.UgcFaq;
import com.plant.entity.UgcFaqAnswer;
import com.plant.entity.UgcNewthings;
import com.plant.entity.UgcTopicJoin;
import com.plant.entity.UgcUploadImages;
import com.plant.service.NewThingsService;
import com.plant.tool.BuildString;
import com.plant.tool.ImageZipUtil;

/**
 * @author JAVA工程师 ---李琦
 * 
 *
 */
@Controller
@RequestMapping(value="/ugcuser")
public class NewThingsAction {
	@Resource(name = "newThingsServiceImpl")
    private NewThingsService newThingsService;
	
	
	@RequestMapping(value="/newthings")  //1--用户发布新鲜事
	@ResponseBody
	public Map<Object,Object> newthings(HttpServletResponse response,Integer user_id,UgcNewthings th,@RequestParam(value="remind_who[]",required=false) String[] id,@RequestParam(value="file",required=false) MultipartFile[] myfiles,String secret,String pust_val,HttpServletRequest request){
		response.setHeader("access-control-allow-origin","*");
		Map<Object,Object> map=new HashMap<Object, Object>();
		if(secret==null||pust_val==null||th.getContent()==null){
			map.put("message", "参数为空");
			map.put("state", 0);
			return map;
		}else{
			Md md=new Md();
			String mdsc=md.md5calc(pust_val+"flower_duigu2016"); 
			if(secret.endsWith(mdsc)){
				th.setUserId(user_id);
				BuildString build=new BuildString();
				if(id!=null){
				for (int i = 0; i < id.length; i++) {
					if(id[i]==null){
						th.setRemindUserId("");
					}else{
						String remind_user_id=build.StringBuilder(id);//处理数组id变成String
						th.setRemindUserId(remind_user_id);
					}	
				}
				}else{
					System.out.println("提醒谁为空"+id);
				}
				SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			   	String t=sim.format(new Date()); 
				GetIp getip=new GetIp();
			   	String ip=getip.getIp2(request);
				th.setIp(ip);
				th.setCreateTime(Timestamp.valueOf(t));
				th.setPv(0);
				boolean b=newThingsService.addnewthings(th);
				if(myfiles!=null){
				   for(MultipartFile myfile : myfiles){  
			            if(myfile.isEmpty()){  
			                System.out.println("图片为空");
			            }else{  
			                System.out.println("图片地址: " + myfile.getSize());  
			                System.out.println("图片类型: " + myfile.getContentType());  
			                System.out.println("图片名称: " + myfile.getName());  
			                System.out.println("图片原名称: " + myfile.getOriginalFilename());  
			                System.out.println("========================================");  
			                String fileName=myfile.getOriginalFilename();
			                fileName = fileName.substring(fileName.lastIndexOf("\\")+1);
			                String extName = fileName.substring(fileName.lastIndexOf("."));//.jpg
			                String uuid = UUID.randomUUID().toString().replace("-", "");
			                String newName = uuid+extName;   
			                String realPath = request.getSession().getServletContext().getInitParameter("UPLOAD_IMAGE_PATH_NEWTHINGS");//存取图片路径
			                try {
								org.apache.commons.io.FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(realPath,newName));
							} catch (IOException e) {
								e.printStackTrace();
							}  
							String url="http://www.zhiwuxiansheng.com:8080/images/newthings/"+newName;
			                ImageZipUtil img=new ImageZipUtil();
			                img.zipImageFile(new File(realPath,newName), new File(realPath,newName), 1080, 638, 0.6f);//发布新鲜事压缩
			                UgcUploadImages upi=new UgcUploadImages();
							upi.setCreateTime(Timestamp.valueOf(t));
							upi.setIp(ip);
							upi.setFromContentType(1);
							upi.setUserId(th.getUserId());
							upi.setFromContentId(th.getId());
							upi.setUrl(url);
							newThingsService.addnewthingsurl(upi);//用户发布新鲜事
			            }  
			        }
				}else{
					System.out.println("图片参数为空"+myfiles);
				}
				if(b){
					map.put("state", 1);
					map.put("message", "发布新鲜事成功");
					return map;
				}else{
					map.put("state", 0);
					map.put("message", "发布新鲜事失败");
					return map;
				}
			}else{
				map.put("message", "秘钥错误");
				map.put("state", 0);
				return map;	
			}
		}
	}
	
	
	@RequestMapping(value="delmythings")//-----2.删除个人动态
	@ResponseBody
	public Map<Object,Object> deleteMyThings(HttpServletResponse response,Integer id_key,Integer user_id,Integer Content_type,String secret,String pust_val,UgcNewthings uth,UgcFaq ufaq,UgcFaqAnswer ufaA,UgcTopicJoin utj){
		response.setHeader("access-control-allow-origin","*");
		Map<Object,Object> map=new HashMap<Object,Object>();
    	if(secret==null||pust_val==null||Content_type==null||id_key==null||user_id==null){
    		map.put("message", "参数为空");
    		map.put("state", 0);
    		return map;
		    	}else{
			    Md md=new Md();
		    	String mds=md.md5calc(pust_val+"flower_duigu2016");
		    	if(secret.endsWith(mds)){
//					Integer id_key=1;
//					Integer user_id=92;     
//					Integer Content_type = 4;
					if(id_key!=null&&Content_type==1){
							uth.setId(id_key);
							uth.setUserId(user_id);
							boolean b=newThingsService.deleteNewThings(uth);		
							if(b){
								map.put("message", "删除新鲜事成功");
								map.put("state", 1);
								newThingsService.delNewThingphoto(uth);
							}else{
								map.put("message", "删除新鲜事失败");
								map.put("state", 0);
							}
							return map;	
					}else if(id_key!=null&&Content_type==3){
							ufaq.setId(id_key);
							ufaq.setUserId(user_id);
							boolean b=newThingsService.delfaq(ufaq);
							if(b){
								map.put("message", "删除求助成功");
								map.put("state", 1);
								newThingsService.delfaqphoto(ufaq);
							}else{
								map.put("message", "删除求助失败");
								map.put("state", 0);
							}
							return map;	
							
					}else if(id_key!=null&&Content_type==5){
							ufaA.setUserId(user_id);
							ufaA.setId(id_key);
							boolean b=newThingsService.delfaqAnswer(ufaA);
							if(b){
								newThingsService.delMyAnswerphoto(ufaA);
								map.put("message", "删除求助回答成功");
								map.put("state", 1);
							}else{
								map.put("message", "删除求助回答失败");
								map.put("state", 0);
							}
							return map;
							
					}else if(id_key!=null&&Content_type==4){
							utj.setId(id_key);
							utj.setUserId(user_id);
							boolean b=newThingsService.delTopicJoin(utj);
							if(b){
								map.put("message", "删除话题参与讨论成功");
								map.put("state", 1);
							}else{
								map.put("message", "删除话题参与讨论失败");
								map.put("state", 0);
							}
							return map;
							}
							map.put("message", "删除成功");
							return map;
				  }else{
			    			map.put("message", "秘钥错误");
			    			map.put("state", 0);
			    			return map;
				 }
    	}
	}
	
	
}
